package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.util.StringUtils;
import com.music.dao.MusicDao;
import com.music.dao.PlaylistDao;
import com.music.pojo.Music;
import com.music.pojo.Playlist;
import com.music.service.PlaylistService;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class PlaylistServiceImpl implements PlaylistService {

    @Autowired
    private PlaylistDao playlistDao;

    @Override
    public int insert(Playlist playlist, String tagStr) {
        int result = 0;
        try {
            // 通过id查找歌单，不存在则添加
            if (playlistDao.getByPrimaryKey(playlist.getId()) == null) {
                // 1. 创建歌单的时间
                playlist.setCreateTime(new Date(new java.util.Date().getTime()));
                // 修改歌单时间
                playlist.setUpdateTime(new Date(new java.util.Date().getTime()));
                // 2.播放次数,新创建的歌单播放次数都为0
                playlist.setPlayCount(1);
                // 3.状态：正常、下架
                playlist.setStatus(1);
                // 设置标签和歌单的关联
                // 1、对TagStr做非空判断
                if (StringUtils.isNotEmpty(tagStr)) {
                    List<Integer> tagIds = new ArrayList<>();
                    // 2、以，进行切割
                    for (String tag : tagStr.split(",")) {
                        tagIds.add(Integer.parseInt(tag));
                    }
                    // 标签id放入歌单实体类
                    playlist.setTags(tagIds);
                    // 将歌单id和标签id存入歌单标签库
                    for (int i = 0;i < tagIds.size();i++){
                        playlistDao.insertPlaylistAndTag(playlist.getId(),tagIds.get(i));
                    }
                }
                // 歌单名不为空且不为空字符串
                if (StringUtils.isNotEmpty(playlist.getName())) {
                    result = playlistDao.insert(playlist);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int delete(Long id) {
        //  对传入的id做非空判断
        if (StringUtils.isNotEmpty(id)) {
            try {
                return playlistDao.delete(id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int update(Playlist playlist) {
        // 修改功能
        int result = 0;
        // 对歌单的id做非空判断
        if (StringUtils.isNotEmpty(playlist.getId())) {
            // 歌单名不为空且不为空字符串
            if (StringUtils.isNotEmpty(playlist.getName())) {
                try {
                    result = playlistDao.update(playlist);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    @Override
    public Playlist getByPrimaryKey(Long id) {
        // 对传入的id做非空判断
        if (StringUtils.isNotEmpty(id)) {
            try {
                return playlistDao.getByPrimaryKey(id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<Playlist> listPage(Playlist playlist, int page, int size) {
        try {
            return playlistDao.listPage(playlist, page, size);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Playlist> listPageInfo(Playlist playlist, int page, int size) {
        try {
            return playlistDao.listPageInfo(playlist, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Music> pageById(Long playlistId) {
        List<Music> musicList = null;
        if (StringUtils.isNotEmpty(playlistId)) {
            // 通过歌单id查找
            try {
                // 先判断传过来的歌单id存在,存在才能查询歌单里面的歌曲
                if (playlistDao.getByPrimaryKey(playlistId) != null) {
                    musicList = playlistDao.selectMusicById(playlistId);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return musicList;
    }

    @Override
    public int insertMusicToPlayList(Long playlistId, Integer id) {
        int result = 0;
        // 先将歌单id和歌曲id判空
        if (StringUtils.isNotEmpty(playlistId) && StringUtils.isNotEmpty(id)){
            try {
                // 在判断这个歌曲是否在这个歌单中,不存在这个歌单中，则进行添加功能
                if (playlistDao.checkMusicFromPlaylist(playlistId,id) == null) {
                    result = playlistDao.insertMusicToPlayList(playlistId, id);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public List<Playlist> selectByExample(Playlist playlist) {
        Example example = createExmaple(playlist);
        try {
            return playlistDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }



    private Example createExmaple(Playlist playlist) {
        Example example = new Example(Playlist.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if (playlist != null) {
            if (!StringUtils.isEmpty(playlist.getId())) {
                criteria.andEqualTo("id", playlist.getId());
            }
            if (!StringUtils.isEmpty(playlist.getUserId())) {
                criteria.andEqualTo("userId", playlist.getUserId());
            }
            if (!StringUtils.isEmpty(playlist.getName())) {
                criteria.andLike("name", "%" + playlist.getName() + "%");
            }
            if (!StringUtils.isEmpty(playlist.getDescription())) {
                criteria.andEqualTo("description", playlist.getDescription());
            }
            if (!StringUtils.isEmpty(playlist.getCoverImgUrl())) {
                criteria.andEqualTo("coverImgUrl", playlist.getCoverImgUrl());
            }
            if (!StringUtils.isEmpty(playlist.getPlayCount())) {
                criteria.andEqualTo("playCount", playlist.getPlayCount());
            }
            if (!StringUtils.isEmpty(playlist.getCreateTime())) {
                criteria.andEqualTo("createTime", playlist.getCreateTime());
            }
            if (!StringUtils.isEmpty(playlist.getUpdateTime())) {
                criteria.andEqualTo("updateTime", playlist.getUpdateTime());
            }
            if (!StringUtils.isEmpty(playlist.getStatus())) {
                criteria.andEqualTo("isDeleted", playlist.getStatus());
            }
        }
        return example;
    }

    /**
     * Playlist 构建查询对象
     *
     * @param playlist
     * @return
     */
    public Example createExample(Playlist playlist) {
        Example example = new Example(Playlist.class);
        Example.Criteria criteria = example.createCriteria();
        if (playlist != null) {
            if (!StringUtils.isEmpty(playlist.getId())) {
                criteria.andEqualTo("id", playlist.getId());
            }
            if (!StringUtils.isEmpty(playlist.getUserId())) {
                criteria.andEqualTo("userId", playlist.getUserId());
            }
            if (!StringUtils.isEmpty(playlist.getName())) {
                criteria.andLike("name", "%" + playlist.getName() + "%");
            }
            if (!StringUtils.isEmpty(playlist.getDescription())) {
                criteria.andEqualTo("description", playlist.getDescription());
            }
            if (!StringUtils.isEmpty(playlist.getCoverImgUrl())) {
                criteria.andEqualTo("coverImgUrl", playlist.getCoverImgUrl());
            }
            if (!StringUtils.isEmpty(playlist.getPlayCount())) {
                criteria.andEqualTo("playCount", playlist.getPlayCount());
            }
            if (!StringUtils.isEmpty(playlist.getCreateTime())) {
                criteria.andEqualTo("createTime", playlist.getCreateTime());
            }
            if (!StringUtils.isEmpty(playlist.getUpdateTime())) {
                criteria.andEqualTo("updateTime", playlist.getUpdateTime());
            }
            if (!StringUtils.isEmpty(playlist.getStatus())) {
                criteria.andEqualTo("isDeleted", playlist.getStatus());
            }
        }
        return example;
    }
}
